package com.google.gwt.sample.json.client;

import com.google.gwt.sample.json.client.Header.JSONResponseTextHandler;
import com.google.gwt.sample.json.client.SearchResult.JSONCountResponseTextHandler;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ui.*;

public class HeaderTab extends Composite {
	
	private class TabLabel extends Label{
		private String searchString;
		public TabLabel(String title, String searchString){
			super(title);
			this.searchString = searchString;
		}
		private String SearchText(){return searchString;} 
	}
	private abstract class TabMouseListener implements MouseListener{
		protected TabLabel label;
		private String style;
		public TabMouseListener(TabLabel label, String style){
			this.label = label;
			this.style = style;
		}
//		public void onMouseDown(final Widget sender, final int x, final int y) {		
//			HTTPRequest.asyncGet(label.SearchText(), new JSONResponseTextHandler(new WorkshopSearchResult()));
//		}
		public void onMouseUp(final Widget sender, final int x, final int y) {}
		public void onMouseMove(final Widget sender, final int x, final int y) {}
		public void onMouseEnter(final Widget sender) {
			label.setStyleName(style);
		}
		public void onMouseLeave(final Widget sender) {
			if (current != style)
				label.setStyleName("gwt-Label");
		}
	}
	private class WorkshopTabMouseListener extends TabMouseListener{
		public WorkshopTabMouseListener(TabLabel label, String style){
			super(label, style);
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {
  		    HTTPRequest.asyncGet("/python/workshops/count", new JSONCountResponseTextHandler());		        	  
			HTTPRequest.asyncGet(label.SearchText(), new JSONResponseTextHandler(new WorkshopSearchResult(true), tb.getText(), true));
		}			
	}
	private class CodeTabMouseListener extends TabMouseListener{
		public CodeTabMouseListener(TabLabel label, String style){
			super(label, style);
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {		
  		    HTTPRequest.asyncGet("/python/codesamples/count", new JSONCountResponseTextHandler());		        	  
			HTTPRequest.asyncGet(label.SearchText(), new JSONResponseTextHandler(new CodeSearchResult(true), tb.getText(), true));
		}			
	}


	public void setSearchBox(TextBox tb){
		this.tb = tb;
	}
	public HeaderTab() {
		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		horizontalPanel_3.setSize("200px", "20px");
		
		initWidget(horizontalPanel_3);
		
		final TabLabel workshopsLabel = new TabLabel("Workshops", "/python/workshops?search=");
		workshopsLabel.addMouseListener(new WorkshopTabMouseListener(workshopsLabel, "gwt-WorkshopLabel"));
		horizontalPanel_3.add(workshopsLabel);
		horizontalPanel_3.setCellVerticalAlignment(workshopsLabel, HasVerticalAlignment.ALIGN_MIDDLE);
		workshopsLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		workshopsLabel.setSize("75px", "25px");
		workshopsLabel.setStyleName("gwt-WorkshopLabel");

		final TabLabel materialsLabel = new TabLabel("Materials", "/python/workshops?search=");
		materialsLabel.addMouseListener(new WorkshopTabMouseListener(materialsLabel, "gwt-MaterialLabel"));
		horizontalPanel_3.add(materialsLabel);
		horizontalPanel_3.setCellVerticalAlignment(materialsLabel, HasVerticalAlignment.ALIGN_MIDDLE);
		materialsLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		materialsLabel.setSize("75px", "25px");

		final TabLabel themesLabel = new TabLabel("Themes", "/python/workshops?search=");
		themesLabel.addMouseListener(new WorkshopTabMouseListener(themesLabel, "gwt-ThemesLabel"));
		horizontalPanel_3.add(themesLabel);
		horizontalPanel_3.setCellVerticalAlignment(themesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
		themesLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		themesLabel.setSize("75px", "25px");

		final TabLabel codeLabel = new TabLabel("Code", "/python/codesamples?search=");
		codeLabel.addMouseListener(new CodeTabMouseListener(codeLabel, "gwt-CodeLabel"));
		horizontalPanel_3.add(codeLabel);
		horizontalPanel_3.setCellVerticalAlignment(codeLabel, HasVerticalAlignment.ALIGN_MIDDLE);
		codeLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		codeLabel.setSize("75px", "25px");
		
	}
	private String current = "gwt-WorkshopLabel";
	private TextBox tb;
}
